package com.servlet;

import com.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class CookieServlet extends BaseServlet{

    protected void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、创建cookie对象
        Cookie cookie = new Cookie("key2","value2");
        //2、通知客户端保存cookie，这里不能省略，否则客户端无法接收
        resp.addCookie(cookie);

        //可以一次创建多个cookie
        Cookie cookie1 = new Cookie("key3","value3");
        resp.addCookie(cookie1);
        resp.getWriter().write("cookie创建成功");
    }

    protected void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();//获取cookie数组

        for (Cookie cookie:cookies) {
            //getName()返回cookie的名称,getValue()返回cookie的value值
            resp.getWriter().write("cookie["+cookie.getName()+"="+cookie.getValue()+"]<br/>");
        }
        Cookie iWantCookie = CookieUtils.findCookie("key1",cookies);
//        for (Cookie cookie:cookies) {
//            if ("key2".equals(cookie.getName())){
//                iWantCookie = cookie;
//                break;
//            }
//        }
        //如果不等于null，也就是赋过值，也就是找到了Cookie
        if (iWantCookie != null){
            resp.getWriter().write("找到了需要的Cookie:"+iWantCookie.getName()+":"+iWantCookie.getValue());
        }
    }

    //Cookie的修改
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1、先创建一个同名的Cookie对象
//        //2、在构造器同时赋值新值给Cookie值
//        Cookie cookie = new Cookie("key1","newValue1");
//        //3、调用response.addCookie(Cookie);通知客户端保存修改
//        resp.addCookie(cookie);
//        resp.getWriter().write("key1的值已经修改好："+cookie.getName()+":"+cookie.getValue());


        //1、先查找到需要修改的 Cookie 对象
        Cookie cookie = CookieUtils.findCookie("key2",req.getCookies());
        if (cookie!=null){
            //2、调用 setValue()方法赋于新的 Cookie 值。
            cookie.setValue("newValue2");
            //3、调用 response.addCookie()通知客户端保存修改
            resp.addCookie(cookie);
            resp.getWriter().write("key2的值已经修改好："+cookie.getName()+":"+cookie.getValue());
        }
    }

    //默认情况，浏览器关闭后删除
    protected void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLift","defaultLife");
        cookie.setMaxAge(-1);//设置存活时间
        resp.addCookie(cookie);
    }

    //存活时间为0，立刻删除
    protected void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先找到需要删除的cookie对象
        Cookie cookie = CookieUtils.findCookie("key2",req.getCookies());
        if (cookie!=null){
            //调用setMaxAge(0)
            cookie.setMaxAge(0);//表示马上删除，不需要等待浏览器关闭
            //调用response.addCookie(cookie)
            resp.addCookie(cookie);
            resp.getWriter().write("key2的cookie已经被删除");
        }
    }

    //存活一个小时
    protected void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie=new Cookie("life3600","life3600");
        cookie.setMaxAge(60*60);//设置Cookie一小时后删除，无效
        resp.addCookie(cookie);
        resp.getWriter().write("已经创建一个存活一小时的cookie");
    }

    protected void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("path1","path1");
        //req.getContextPath()===>得到工程路径
        cookie.setPath(req.getContextPath()+"/abc");//====> /工程路径/abc
        resp.addCookie(cookie);
        resp.getWriter().write("创建了一个带有path路径的cookie");
    }
}
